// packageA/member/redPacket/redPacket.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    detail: {
      value: "",
      type_name: "",
      ad_link: "",
      id: ""
    },
    list:[],
    current_page:1,
    last_page:0
  },
  showOverlay(event) {
    let id = event.currentTarget.dataset.id;
    this.setData({
      show: true
    })

    this.getDetail(id)
  },
  getDetail(id){
    let urlStr =  app.getNetAddresss('plugin.love-speed-pool.frontend.redpack.detail')
    app._getNetWork({
      url:urlStr,
      data:{
        lr_id:id
      },
      success: (res) => {
        if (res.data.result == 1) {
          let data = res.data.data.detail;
          this.setData({
            "detail.value": data.value,
            "detail.type_name":data.type_name,
            "detail.ad_link":data.ad_link,
            "detail.id":data.id
          })
        } else {
          app.tips(res.data.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  confirm(){
    app._getNetWork({
      url: app.getNetAddresss('plugin.love-speed-pool.frontend.redpack.receive'),
      data:{
        lr_id:this.data.detail.id
      },
      success: (res) => {
        if (res.data.result == 1) {
          this.getData()
          app.tips(res.data.msg)
        } else {
          app.tips(res.data.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  onClickHide() {
    this.setData({
      show: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()
  },
  getData() {
    app._getNetWork({
      url: app.getNetAddresss('plugin.love-speed-pool.frontend.redpack.index'),
      success: (res) => {
        let data = res.data.data.list;
        if (res.data.result == 1) {
          this.setData({
            list: data.data,
            current_page:data.current_page,
            last_page:data.last_page
          })
          wx.setNavigationBarTitle({
            title: res.data.data.diy_names.diy_receive_red_action,
          })
        } else {
          app.tips(res.data.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.current_page>=this.data.last_page){
      return false;
    }
    let page = this.data.current_page+1;
    app._getNetWork({
      url: app.getNetAddresss('plugin.love-speed-pool.frontend.redpack.index'),
      data:{
        page
      },
      success: (res) => {
        if (res.data.result == 1) {
          let list = this.data.list.concat(res.data.data.list.data);
          this.setData({
            list,
            current_page:res.data.data.list.current_page,
            last_page:res.data.data.list.last_page
          })
        } else {
          app.tips(res.data.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})